﻿using AutoMapper;
using PeiYue.IRepository.Base;
using PeiYue.IServices.Tms;
using PeiYue.Model;
using PeiYue.Model.Dto.Tms;
using PeiYue.Model.Models.Tms;
using PeiYue.Services.BASE;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PeiYue.Services.Tms
{
    public class DriverServices : BaseServices<DriverDao>, IDriverServices
    {
        IBaseRepository<DriverDao> _dal;
        IMapper _mapper;
        public DriverServices(IBaseRepository<DriverDao> dal, IMapper mapper)
        {
            this._dal = dal;
            base.BaseDal = dal;
            this._mapper = mapper;
        }
        /// <summary>
        /// 获取司机分页列表
        /// </summary>
        /// <param name="requestDto"></param>
        /// <returns></returns>
        public async Task<PageModel<SearchDriverResponseDto>> GetPage(SearchDriverRequestDto requestDto)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select d.*,u1.namec as create_names,u2.namec as update_names, ");
            strSql.Append("v.names as vc_name,v.codec as vc_codec ");
            strSql.Append("from oms_wms_driver d ");
            strSql.Append("left join oms_ur_user u1 on d.create_user=u1.id ");
            strSql.Append("left join oms_ur_user u2 on d.update_user=u2.id ");
            strSql.Append("left join oms_wms_vehicle v on d.vc_id=v.id ");
            strSql.Append("where 1=1 ");
            List<SugarParameter> parameters = new List<SugarParameter>();
            if (requestDto.vc_id.HasValue&&requestDto.vc_id > 0)
            {
                strSql.Append("and d.vc_id=@vc_id ");
                parameters.Add(new SugarParameter("@vc_id", requestDto.vc_id));
            }
            if (!string.IsNullOrWhiteSpace(requestDto.phones))
            {
                strSql.Append("and d.phones like @phones ");
                parameters.Add(new SugarParameter("@phones", "%"+requestDto.phones+ "%"));
            }
            if (!string.IsNullOrWhiteSpace(requestDto.names))
            {
                strSql.Append("and d.names like @names ");
                parameters.Add(new SugarParameter("@names", "%" + requestDto.names + "%"));
            }
            if (requestDto.status.HasValue && requestDto.status > 0)
            {
                strSql.Append("and d.status=@status ");
                parameters.Add(new SugarParameter("@status", requestDto.status));
            }
            else
            {
                strSql.Append("and d.status<3 ");
            }
            if (!string.IsNullOrWhiteSpace(requestDto.timef))
            {
                strSql.Append("and d.create_time>=@create_time_begin ");
                parameters.Add(new SugarParameter("@create_time_begin", requestDto.timef));
            }
            if (!string.IsNullOrWhiteSpace(requestDto.timet))
            {
                strSql.Append("and d.create_time<@create_time_end ");
                parameters.Add(new SugarParameter("@create_time_end", Convert.ToDateTime(requestDto.timet).AddDays(1)));
            }
            return await base.SqlQueryPage<SearchDriverResponseDto>(strSql.ToString(), parameters, requestDto.pageIndex, requestDto.pageSize, " d.id desc ");
        }
       
    }
}
